/**
 * 
 */
package com.haizhi.user.controller;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.haizhi.bean.TbProduct;
import com.haizhi.service.ProductService;
import com.haizhi.user.vo.ProductEasyVo;
import com.haizhi.util.BaseVoUtil;
import com.haizhi.util.MyMapBuilder;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

/**
 * @author jiangjintai
 *
 */
@Controller
@RequestMapping("/index")
@Api(description="首页版块", value = "首页版块")//添加注释  
public class IndexController {
	@Autowired
	ProductService productService;
	@RequestMapping("/getPopularList")
	@ApiOperation(value = "获取全部收欢迎的作品",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> getPopularList(
			@ApiParam(value="页码数，开始为0，默认为0",name="page",required=true)@RequestParam(defaultValue="0",name="page")String page,
			@ApiParam(value="每页数量，默认20",name="size",required=true)@RequestParam(defaultValue="20",name="size")String size,
			@PageableDefault Pageable pageAble
			) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		Page<TbProduct> tbProductPage = productService.findByProductIsExcellent(1,pageAble);
		List<ProductEasyVo> productVoList = new ArrayList<ProductEasyVo>();
		List<TbProduct> productList=tbProductPage.getContent();
		for(TbProduct tbProduct : productList){
			ProductEasyVo productVo = BaseVoUtil.getVo(tbProduct, new ProductEasyVo());
			productVo.setUserId(tbProduct.getTbUser().getUserId());
			productVo.setUserName(tbProduct.getTbUser().getUserName());
			productVo.setUserSex(tbProduct.getTbUser().getUserSex());
			productVoList.add(productVo);
		}
		return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildSuccessMap("添加成功").put("product", productVoList),HttpStatus.OK);
	}
}
